const mongoose = require('../utils/db')

const userSchema = new mongoose.Schema({
    username:String,
    password:String,
    avatar:String
})

var User = mongoose.model('users',userSchema)

//插入一个用户
const insertUser = (userinfo) =>{
    const user = new User(userinfo)
    return new Promise((resolve,reject)=>{
        user.save((err,docs) =>{
            if(err) reject(err)
            else resolve(docs)
        })
    })
}

//查找用户
const isUserExist = (userinfo) =>{
    return new Promise((resolve,reject) =>{
        User.find(userinfo,(err,doc)=>{
            if(err) reject(err)
            else resolve(doc.length > 0)
        })
    })
}

//注册查找
const isUserLoginExist = (userinfo) =>{
    return new Promise((resolve,reject) =>{
        User.find(userinfo,(err,docs)=>{
            if(err) reject(err)
            else resolve(docs)
        })
    })
}

//方法暴露
module.exports = {
    insertUser,
    isUserExist,
    isUserLoginExist
}